﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Presenters;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
    public partial class Invoice : Form,IInvoiceView
    {
        private InvoicePresenter presenter;
        private Random ran = new Random(DateTime.Now.Millisecond);
        private InvoiceData invoicedata = new InvoiceData();
        private int index = 1;
        private int pagototal = 0;
        public bool save { get; set; }
        public Invoice()
        {
            InitializeComponent();
            save = false;
            presenter = new InvoicePresenter(this);
            lNoValue.Text = ran.Next(int.MaxValue).ToString();
            lDateValue.Text = DateTime.Today.ToString();
        }
        public Invoice(string name,string ci,string price,Collection<Sale> product)
        {
            InitializeComponent();
            save = false;
            presenter = new InvoicePresenter(this);
            lNoValue.Text = ran.Next(int.MaxValue).ToString();
            lDateValue.Text = DateTime.Today.ToString();
            txtName.Text = name;
            txtCI.Text = ci;
            ListViewItem item = new ListViewItem(index.ToString());
            item.SubItems.Add(" PAGO USO HABITACION ");
            item.SubItems.Add(price);
            listDetail.Items.Add(item);
            pagototal += Convert.ToInt32(Convert.ToDecimal(price));
            this.txtTotal.Text = pagototal.ToString();
            ListViewItem sales = new ListViewItem(index.ToString());
            for (int i = 0; i < product.Count; i++)
            {
                Sale sale = product.ElementAt<Sale>(i);
                sales.SubItems.Add(sale.Description + " " + sale.DateOfSale);
                sales.SubItems.Add(sale.CostSaleProducts);
                listDetail.Items.Add(sales);
                pagototal += Convert.ToInt32(sale.CostSaleProducts);
                this.txtTotal.Text = pagototal.ToString();
                ++index;
            }
        }
        #region IInvoiceView Members

        public string Number
        {
            get { return lNoValue.Text; }
        }

        public string FullName
        {
            get { return txtName.Text; }
        }

        public string CI
        {
            get { return txtCI.Text; }
        }

        public DateTime DateInvoice
        {
            get { return DateTime.Today; }
        }

        public Collection<InvoiceDetail> Detail
        {
            get { return InvoiceManager.ConvertToCollection(this.listDetail); }
        }

        #endregion

        #region IBaseView Members

        public string MessageWarnings
        {
            get
            {
                return "All is very good";
            }
            set
            {
                MessageBox.Show(value);
            }
        }

        #endregion

        private void bSave_Click(object sender, EventArgs e)
        {
            presenter.PrintInvoice();
            lNoValue.Text = ran.Next(int.MaxValue).ToString();
            save = true;
            ClearAll();
        }

        private void bPreview_Click(object sender, EventArgs e)
        {
            try
            {
                PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
                printPreviewDialog.Document = this.printDocument;
                printPreviewDialog.FormBorderStyle = FormBorderStyle.Fixed3D;
                printPreviewDialog.SetBounds(20, 20, this.Width, this.Height);
                printPreviewDialog.ShowDialog();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            this.Focus();
            DrawAll(e.Graphics);
        }
        private void DrawAll(Graphics g)
        {
            g.DrawImage(this.pictbok.Image, new Point(70, 110));
            g.DrawRectangle(new Pen(Color.Red),370,100,400,150);
            g.DrawString("INVOICE", new Font("Arial", 27), new SolidBrush(Color.Red), new Point(470, 56));
            g.DrawString("       NIT: 1222554888", new Font("Arial", 16), new SolidBrush(Color.Black), new Point(380, 130));
            g.DrawString("INVOICE No: " + lNoValue.Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(380, 160));
            g.DrawString("      DATE: "+lbelDate.Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(380, 190));
            g.DrawString("  NAME: "+txtName.Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(70, 295));
            g.DrawString("    CI/NIT: "+txtCI.Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(70, 325));
            g.DrawRectangle(new Pen(Color.Red), 60, 370, 710, 500);
            g.DrawLine(new Pen(Color.Red), new Point(60, 410), new Point(770, 410));
            int indexprint = 0;
            g.DrawString("   ID                          CONCEPT                                     MOUNT", new Font("Arial", 16), new SolidBrush(Color.Black), new Point(70, 380));
            foreach (ListViewItem item in listDetail.Items)
            {
                g.DrawString(item.SubItems[0].Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(80, 440 + indexprint));
                g.DrawString(item.SubItems[1].Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(180, 425 + indexprint));
                g.DrawString(item.SubItems[2].Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(650, 440 + indexprint));
                indexprint += 30;
            }
            g.DrawString("TOTAL Bs.: "+txtTotal.Text, new Font("Arial", 16), new SolidBrush(Color.Black), new Point(580, 880));

        }
     
        private void isertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InsertDetail detail = new InsertDetail();
            detail.ShowDialog();
            ListViewItem item = new ListViewItem(index.ToString());
            item.SubItems.Add(detail.GetConcept());
            item.SubItems.Add(detail.GetMount());
            listDetail.Items.Add(item);
            pagototal += Convert.ToInt32(detail.GetMount());
            this.txtTotal.Text = pagototal.ToString();
            index++;
        }

        private void bSelect_Click(object sender, EventArgs e)
        {
            save = false;
            listDetail.Items.Clear();
            listForInvoice list = new listForInvoice();
            list.ShowDialog();
            ListViewItem item = new ListViewItem(index.ToString());
            if (list.user == "employe")
            {
                this.txtCI.Text = list.NIT;
                this.txtName.Text = list.FullName;
                item.SubItems.Add(" PAGO SUELDO MENSUAL ");
                item.SubItems.Add(list.Pago);
                listDetail.Items.Add(item);
                pagototal += Convert.ToInt32(list.Pago);
                this.txtTotal.Text = pagototal.ToString();
            }
            else
            {
                this.txtCI.Text = list.NIT;
                this.txtName.Text = list.FullName;
                item.SubItems.Add(" PAGO POR HOSPEDAJE ");
                item.SubItems.Add(list.Pago);
                listDetail.Items.Add(item);
                pagototal += Convert.ToInt32(list.Pago);
                this.txtTotal.Text = pagototal.ToString();
            }
            
            index++;
        }

        private void bclosed_Click(object sender, EventArgs e)
        {
            if (!save)
            {
                MessageBox.Show("No ha guardado la factura guarde...");
            }
            else
            {
                this.Close();
            }
        }
        private void ClearAll() 
        {
            this.listDetail.Items.Clear();
            this.txtCI.Text = "";
            this.txtName.Text = "";
            this.txtTotal.Text = "";
            this.index = 1;
            this.pagototal = 0;
        }
    }
}
